package com.qianchang.microfinance.view.home.cash;

import android.os.Bundle;
import android.webkit.WebView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.HtmlUtil;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductIntroduceActivity extends BaseActivity {

    @BindView(R.id.tv_product_introduction)
    WebView tvProductIntroduction;
    private String productId;
    private String productType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_introduce);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.Label_product_intrduce));
        productId = getBundle().getString(ConstantAppInfo.PROJECT_ID);
        productType = getBundle().getString(ConstantAppInfo.PROJECT_TYPE);
        tvProductIntroduction = (WebView) findViewById(R.id.tv_product_introduction);
        getData();
    }

    public void getData() {
        show();
        HttpParams params = new HttpParams();
        params.put("productType", productType); //产品类型
        params.put("productId", productId); //产品Id
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.product_desc , params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    String strContent = response.getObject();
                    if (!StringUtils.isEmptyOrNull(strContent)) {
                        tvProductIntroduction.loadDataWithBaseURL(null, strContent, "text/html", "UTF-8", null);
                        HtmlUtil htmlUtil = new HtmlUtil();
                        htmlUtil.setBody(new StringBuilder().append(strContent));
                        tvProductIntroduction.loadDataWithBaseURL(DMConstant.Config.BASE_URL,
                                htmlUtil.createHtml(), "text/html", "utf-8", null);
                    } else {
                        ToastUtil.getInstant().show(ProductIntroduceActivity.this, getString(R.string.data_error));
                    }
                } else {
                    ToastUtil.getInstant().show(ProductIntroduceActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(ProductIntroduceActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(ProductIntroduceActivity.this, msg);
            }
        });
    }
}
